﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibContrib.ExceptionHandling.Design
{
    /// <summary>
    /// Represents a <see cref="EntLibExtensions.Configuration.SqlExceptionWrapHandlerData"/> configuration element. 
    /// </summary>
    public class SqlExceptionWrapHandlerNode : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.Design.ExceptionHandlerNode
    {
        /// <summary>
        /// Initialize a new instance of the <see cref="SqlExceptionWrapHandlerNode"/> class.
        /// </summary>
        public SqlExceptionWrapHandlerNode()
            : this(new EntLibContrib.ExceptionHandling.Configuration.SqlExceptionWrapHandlerData(
                Resources.SqlExceptionWrapHandlerNodeName, 
                ErrorCodeNotConfiguredBehavior.WrapWithDefaultException,
                "", 
                "System.Exception", 
                new NamedElementCollection<ErrorCodeEntryData>()))
        {
        }

        /// <summary>
        /// Initialize a new instance of the <see cref="SqlExceptionWrapHandlerNode"/> class with a <see cref="EntLibExtensions.Configuration.SqlExceptionWrapHandlerData"/> instance.
        /// </summary>
        /// <param name="data">A <see cref="EntLibExtensions.Configuration.SqlExceptionWrapHandlerData"/> instance</param>
        public SqlExceptionWrapHandlerNode(EntLibContrib.ExceptionHandling.Configuration.SqlExceptionWrapHandlerData data)
        {
            if (null == data) throw new ArgumentNullException("data");

            Rename(data.Name);
            this.defaultExceptionMessage = data.DefaultExceptionMessage;
            this.defaultWrapExceptionTypeName = data.DefaultWrapExceptionTypeName;
            foreach (ErrorCodeEntryData errorCode in data.ErrorCodes)
            {
                ErrorCodeEntryNode errorNode = new ErrorCodeEntryNode(errorCode);
                this.AddNode(errorNode);
            }

        }

        /// <summary>
        /// Gets the <see cref="EntLibExtensions.Configuration.SqlExceptionWrapHandlerData"/> this node represents.
        /// </summary>
        /// <value>
        /// The <see cref="EntLibExtensions.Configuration.SqlExceptionWrapHandlerData"/> this node represents.
        /// </value>
        [Browsable(false)]
        public override Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlerData ExceptionHandlerData
        {
            get
            {

                EntLibContrib.ExceptionHandling.Configuration.SqlExceptionWrapHandlerData data =
                    new EntLibContrib.ExceptionHandling.Configuration.SqlExceptionWrapHandlerData(
                        this.Name,
                        this.errorCodeNotConfiguredBehavior,
                        this.defaultExceptionMessage,
                        this.defaultWrapExceptionTypeName, 
                        new NamedElementCollection<ErrorCodeEntryData>());

                foreach (ErrorCodeEntryNode errorCodeNode in this.Nodes)
                {
                    data.ErrorCodes.Add(errorCodeNode.ErrorCodeEntry);
                }
                return data;
            }
        }

        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="SqlExceptionWrapHandlerNode"/> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">
        /// <see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
            }
            base.Dispose(disposing);
        }

        private ErrorCodeNotConfiguredBehavior errorCodeNotConfiguredBehavior;
        /// <summary>
        /// 
        /// </summary>
        /// <value>
        /// 
        /// </value>
        [SRDescription("ErrorCodeNotConfiguredBehaviorDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public ErrorCodeNotConfiguredBehavior ErrorCodeNotConfiguredBehavior
        {
            get { return this.errorCodeNotConfiguredBehavior; }
            set { this.errorCodeNotConfiguredBehavior = value; }
        }


        private System.String defaultExceptionMessage;
        /// <summary>
        /// 
        /// </summary>
        /// <value>
        /// 
        /// </value>
        [SRDescription("DefaultExceptionMessageDescription", typeof(Resources))]
        [SRCategory("CategoryGeneral", typeof(Resources))]
        public System.String DefaultExceptionMessage
        {
            get { return this.defaultExceptionMessage; }
            set { this.defaultExceptionMessage = value; }
        }

        private System.String defaultWrapExceptionTypeName;

        [SRCategory("CategoryGeneral", typeof(Resources))]
        [Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
        [BaseType(typeof(Exception), TypeSelectorIncludes.BaseType)]
        public System.String DefaultExceptionTypeName
        {
            get { return this.defaultWrapExceptionTypeName; }
            set { this.defaultWrapExceptionTypeName = value; }

        }
    }
}
